package controllers;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;

import model.WorldMapModel;
import view.WorldMapGUI;
import client.CliMessage;

/**
 * The Class WorldMapController.
 * this class controls the world map GUI.
 * then a location in the world map is clicked -
 * the location id in the search view is set to the correct value retrieved from the DB.  
 * @author Tomer Elkayam
 */
public class WorldMapController extends AbstractController
{
	
	/** The map view. */
	WorldMapGUI mapView;
	
	/** The map model. */
	WorldMapModel mapModel;
	
	/**
	 * Instantiates a new world map controller.
	 *
	 * @param mapView the map view
	 * @param mapModel the map model
	 */
	public WorldMapController(WorldMapGUI mapView,WorldMapModel mapModel)
	{
		super(mapView, mapModel);
		this.mapModel = mapModel;
		this.mapView = mapView;
		
		//get all locations rom db
		String searchQuary = "search" 	+ "SELECT lnumber"
										+ " FROM location";
		sendToServer(searchQuary);
	}
	
	
	
	/* (non-Javadoc)
	 * @see client.IObserve#handleDBRequestSuccess()
	 */
	@Override
	public void handleDBRequestSuccess() 
	{
		//do nothing
	}
	
	/* (non-Javadoc)
	 * @see client.IObserve#handleNullSearchResult()
	 */
	@Override
	public void handleNullSearchResult() 
	{
		// location table is empty
		mapView.displayInfoMessage("Error: No locations in DB!", "No locations", 0);
	}
	
	/* (non-Javadoc)
	 * @see client.IObserve#handleDBSearchResult(client.CliMessage)
	 */
	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{
		//get the location and save it to the model
		ArrayList<Integer> locations = new ArrayList<Integer>();
		srs = dbResult.getSrs();
		int nRows = srs.size();
		
		//get all tuples
		for(int i = 0 ; i < nRows ; i++)
		{
			// get a tuple
			HashMap<String, String> locationTuple = srs.get(i);
			
			//get the location
			int iLocation = Integer.parseInt(locationTuple.get("LNUMBER"));
			locations.add(iLocation);
		
			//display for developer
			System.out.println(iLocation);
			
			//set zone button listeners
			mapView.addZoneButtonListener(i,new ZoneButtonListener());
			
		}
		
		//add the locations to model
		mapModel.setLocations(locations);
		
	}
	
	/**
	 * The listener for receiving zone button click events.
	 * The object created with this class is registered with a component using the
	 * component's <code>addZoneButtonListener<code> method. When
	 * the zone button click event occurs, that object's appropriate
	 * method is invoked, and the clicked location is being set in the search window
	 *
	 * @see ZoneButtonEvent
	 */
	class ZoneButtonListener extends AbstractListener
	{
		
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			int index = Integer.parseInt(e.getActionCommand());
			Integer location = mapModel.getLocations().get(index);
			mapView.setPaentWindowWithLocation(location.toString());
			mapView.clickBackButton();
		}
	}
	
}
